package stages;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;

import framework.SnowFlake;
import framework.Stage;
import framework.Util;

public class Snow extends Stage {
	
	protected final int MAX_ENTITIES = 400;
	
	protected ArrayList<SnowFlake> flakes;
	
	protected int lastChange;
	
	protected int nextChange;
	
	protected int timeSpent;
	
	public Snow() {
		flakes = new ArrayList<>();
	}

	@Override
	public int getGroundHeight() {
		return 80;
	}

	@Override
	public void init(GameContainer gc) {
		super.init(gc);
		for (int i=0; i < MAX_ENTITIES; i++) {
			flakes.add(i, new SnowFlake());
			flakes.get(i).setX(Util.randomize(-gc.getWidth(), gc.getWidth()));
			flakes.get(i).setY(Util.randomize(-flakes.get(i).getHeight(), -gc.getHeight()));
		}
		timeSpent = 0;
		//changeWind();
	}

	@Override
	public void draw(Graphics g) {
		super.draw(g);
		for (int i=0; i < flakes.size(); i++) {
			flakes.get(i).draw(g);
		}
	}
	
	protected void changeWind() {
		int windY = Util.randomize(500, 2000);
		int windX = Util.randomize(400, 1000);
		for (int i=0; i < flakes.size(); i++) {
			flakes.get(i).setYspeed(windY);
			flakes.get(i).setXspeed(windX);
		}
		lastChange = timeSpent;
		nextChange = timeSpent + Util.randomize(1000, 5000);
	}

	@Override
	public void update(GameContainer gc, int delta) {
		for (int i=0; i < flakes.size(); i++) {
			flakes.get(i).update(gc, delta);
			if (flakes.get(i).getY() > gc.getHeight() - getGroundHeight()) {
				flakes.get(i).setX(Util.randomize(-gc.getWidth(), gc.getWidth()));
				flakes.get(i).setY(-flakes.get(i).getHeight());
			}
		}
		timeSpent += delta;
		if (timeSpent > nextChange) {
			//changeWind();
		}
	}
}
